﻿using ElectronicCommerce.Framework.Configuration;
using ElectronicCommerce.Framework.Configuration.Entities;

namespace ElectronicCommerce.Framework.ExceptionManagement
{
    /// <summary>
    /// 根据ErrorCode从数据库或者XML文件中获取相应的提示信息
    /// </summary>
    public class ExceptionInfo
    {
        private ExManagerConfig m_ExManagerConfig;
        private string DEFAULT_MESSAGE = "出现未知错误，请记录下错误编号后与管理员联系";

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorInfo"/> class.
        /// </summary>
        /// <param name="config">The config.</param>
        public ExceptionInfo(ExManagerConfig config)
        {
            m_ExManagerConfig = config;
        }

        /// <summary>
        /// 根据错误Id获取详细的错误信息
        /// </summary>
        /// <param name="strErrorCode">The STR error code.</param>
        /// <returns></returns>
        public string GetErrorInfoById(string strErrorCode)
        {
            string strResult;
            if (m_ExManagerConfig.ErrorCodeSource == ErrorCodeSource.DB.ToString())
            {
                strResult = GetErrorInfoFormDatabase(strErrorCode);
            }
            else
            {
                strResult = GetErorrInfoFromXml(strErrorCode);
            }
            return strResult;
        }

        /// <summary>
        /// 从数据库获取详细的错误信息
        /// </summary>
        /// <param name="strErrorCode">The STR error code.</param>
        /// <returns></returns>
        private string GetErrorInfoFormDatabase(string strErrorCode)
        {
            // 未实现，但配置文件中已提供了数据库连接所需的信息
            return string.Empty;
        }

        /// <summary>
        /// 从XML文件中获取详细的错误信息
        /// </summary>
        /// <param name="strErrorCode">The STR error code.</param>
        /// <returns></returns>
        private string GetErorrInfoFromXml(string strErrorCode)
        {
            string errorMsg = string.Empty;
            errorMsg = ConfigManager.GetConfig<ErrorInfoConfig>().ErrorInfoList[strErrorCode].String;
            if (!string.IsNullOrEmpty(errorMsg))
            {
                return errorMsg;
            }
            return DEFAULT_MESSAGE;
        }
    }
}
